﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VIT.Web.UI;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;

namespace VIT.Wedding.Components.Home
{
    public partial class List : VITComponent
    {
        public string vId;
        public dynamic Obj;
        public bool isColor;

#region properties
        private string ColorId
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("sColorsId")) ? "0" : Page.Request.Params.Get("sColorsId"); }
        }
        private string StyleId
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("sStylesId")) ? "0" : Page.Request.Params.Get("sStylesId"); }
        }
        private string CatID
        {
            get { return string.IsNullOrEmpty(Page.Request.Params.Get("CatID")) ? "0" : Page.Request.Params.Get("CatID"); }
        }
#endregion

        private void LoadSkinEmptyTop()
        {
            var pars = new Dictionary<string, string>();
            pars["Position"] = "PromotionCenter";
            pars["Width"] = "749";
            pars["Height"] = "50";
            pars["HasLink"] = "false";
            LoadModule(SkinEmptyTop, "Logo", pars);
        }
        private void LoadSkinBanner(string Position)
        {
            var pars = new Dictionary<string, string>();
            pars["Position"] = Position;
            pars["Width"] = "730";
            pars["Height"] = "300";
            LoadModule(SkinBanner, "SlideshowNumber", pars);
        }
        private void LoadSkinBannerProduct(string catId)
        {
            if (!string.IsNullOrEmpty(catId))
            {
                var pars = new Dictionary<string, string>();
                pars["Id"] = catId;
                LoadModule(SkinBannerProduct, "CategoryDetail", pars);
            }
        }
        private void LoadSkinCenter(string catID, string catName)
        {
            if (!string.IsNullOrEmpty(catID))
            {
                var pars = new Dictionary<string, string>();
                pars["Top"] = "0";
                pars["PageSize"] = "8";
                pars["Category"] = catID;
                pars["InChildCat"] = "true";
                pars["HasFavourite"] = "false";
                pars["Color"] = ColorId;
                pars["Style"] = StyleId;
                LoadModule(SkinCenter, "Products", pars, catName);
            }
        }
        private void LoadSkinChooseType(string ColorNStyleModuleName, string Appropriate)
        {
            var pars = new Dictionary<string, string>();
            pars["ColorNStyleModuleName"] = ColorNStyleModuleName;
            pars["Appropriate"] = Appropriate;
            LoadModule(SkinChooseType, "ColorNStyleScroll", pars);
        }

        private void loadProduct(int catId = 2)
        {
            VITData.OpenTransaction();
            var cats_Lang = VITData.GetQuery<Category>(c => c.ParentId == catId && c.Deleted == false && c.Published == true)
                .Join(VITData.GetQuery<Category_Language>(c => c.LanguageId == Session["language"].ToString()), c => c.Id, c => c.CategoryId, (c, d) => d).ToArray();
            VITData.RollbackTransaction();
            foreach (var item in cats_Lang)
            {
                LoadSkinCenter(item.CategoryId.ToString(), item.Title);
            }
        }

        private void loadChoose(int Id, bool iscolor)
        {
            if (iscolor) Obj = VITData.GetObject<Product_Color>(o => o.Id == Id);
            else Obj = VITData.GetObject<Product_Style>(o => o.Id == Id);
        }

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            base.Page_PreInit(sender, e);
            if (ColorId == "0" && StyleId == "0")
            {
                LoadSkinEmptyTop();
                LoadSkinBannerProduct(CatID);
                LoadSkinBanner("BannerProducts");

                loadProduct(Convert.ToInt32(CatID));
            }
            else
            {
                int Id;
                if (ColorId != "0")
                {
                    isColor = true;
                    LoadSkinBannerProduct("54");
                    LoadSkinChooseType("Styles", ColorId);
                    if (int.TryParse(ColorId, out Id)) loadChoose(Id, isColor);
                }
                else
                {
                    isColor = false;
                    LoadSkinBannerProduct("55");
                    LoadSkinChooseType("Colors", StyleId);
                    if (int.TryParse(StyleId, out Id)) loadChoose(Id, isColor);
                }

                loadProduct();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            vId = Request["sId"];
        }
    }
}